package org.webdb.jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Iterator;

public class JDBCUtils {

    public static void close(ResultSet resultSet) {
        try {
            if (resultSet != null)
                resultSet.close();
        } catch (Exception e) {
        }
    }

    public static void close(Connection connection) {
        try {
            if (connection != null && !connection.isClosed())
                connection.close();

        } catch (Exception e) {
        }
    }

    public static void close(Statement stmt) {
        try {
            if (stmt != null && stmt.isClosed())
                stmt.close();
        } catch (Exception e) {
        }
    }

    public static String getColumnNames(JDBCTable jdbcTable) {
        Iterator<JDBCColumn> it = jdbcTable.getColumns().iterator();
        StringBuilder sb = new StringBuilder();
        while (it.hasNext()) {
            sb.append(it.next().getName());
            if (it.hasNext())
                sb.append(", ");
        }
        return sb.toString();
    }
}
